class CodePiecesController < ApplicationController
  before_filter :login_required, :except => [:index, :show]
  
  layout 'main'
  
  # GET /code_pieces
  # GET /code_pieces.xml
  def index
    @code_pieces = CodePiece.paginate :page => params[:page], :per_page => 5, :order => 'created_at DESC'

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @code_pieces }
      format.rss  { render :layout => false }
    end
  end

  # GET /code_pieces/1
  # GET /code_pieces/1.xml
  def show
    @code_piece = CodePiece.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @code_piece }
    end
  end

  # GET /code_pieces/new
  # GET /code_pieces/new.xml
  def new
    @code_piece = CodePiece.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @code_piece }
    end
  end

  # GET /code_pieces/1/edit
  def edit
    @code_piece = CodePiece.find(params[:id])
  end

  # POST /code_pieces
  # POST /code_pieces.xml
  def create
    @code_piece = CodePiece.new(params[:code_piece])

    respond_to do |format|
      if @code_piece.save
        flash[:notice] = 'Кодо-кусочек создан.'
        format.html { redirect_to(@code_piece) }
        format.xml  { render :xml => @code_piece, :status => :created, :location => @code_piece }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @code_piece.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /code_pieces/1
  # PUT /code_pieces/1.xml
  def update
    @code_piece = CodePiece.find(params[:id])

    respond_to do |format|
      if @code_piece.update_attributes(params[:code_piece])
        flash[:notice] = 'Кодо-кусочек обновлен.'
        format.html { redirect_to(@code_piece) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @code_piece.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /code_pieces/1
  # DELETE /code_pieces/1.xml
  def destroy
    @code_piece = CodePiece.find(params[:id])
    @code_piece.destroy

    respond_to do |format|
      format.html { redirect_to(code_pieces_url) }
      format.xml  { head :ok }
    end
  end
end
